package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.support.toast.ToastUtil;

import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.ChangePwdActivity;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.req.ChangePwdReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Utils;

public class ChangePwdFragment extends NewPwdFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        binding.old.setVisibility(View.VISIBLE);
        binding.tv.setText("Ingrese la nueva contraseña");
        binding.pwd.setHint("Ingrese la nueva contraseña");
        binding.tv1.setText("Confirme la nueva contraseña");
        binding.pwd2.setHint("Confirme la nueva contraseña");
        binding.top.back.setOnClickListener(v -> getActivity().finish());

        binding.oldEye.setSelected(false);
        binding.oldEye.setOnClickListener(v -> {
            boolean bo = !binding.oldEye.isSelected();
            binding.oldEye.setSelected(bo);
            if (bo) {
                binding.oldEye.setImageResource(R.mipmap.eye_open);
                binding.oldPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                binding.oldEye.setImageResource(R.mipmap.eye_close);
                binding.oldPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        });
        Utils.setEditTextInputSpace(binding.oldPwd, 20);

        binding.oldPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.oldErr.setVisibility(View.GONE);
                check();
            }
        });
//        binding.forget.setVisibility(View.VISIBLE);
//        binding.forget.setOnClickListener(v -> {
//            ((ChangePwdActivity) getActivity()).toForget();
//        });

        return view;
    }

    @Override
    protected boolean check() {
        boolean bo = super.check();
        if (!bo) {
            return false;
        }
        String tmp = binding.oldPwd.getText().toString().trim();
        if (tmp.length() < 6) {
            binding.next.setAlpha(0.6f);
            return false;
        }
        binding.next.setAlpha(1f);
        return true;
    }

    @Override
    protected boolean isValid() {
        boolean bo = super.isValid();
        String old = binding.oldPwd.getText().toString().trim();
        if (TextUtils.isEmpty(old)) {
            binding.oldErr.setVisibility(View.VISIBLE);
            binding.oldErr.setText("Ingrese la contraseña actual");
            return false;
        } else {
            binding.oldErr.setVisibility(View.GONE);
        }
        return bo;
    }

    @Override
    protected void send() {
        showLoading();
        String old = binding.oldPwd.getText().toString().trim();
        String now = binding.pwd.getText().toString().trim();
        new ChangePwdReq(old, now).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                if (Constant.white || success && bean.isValid()) {
                    ToastUtil.showShortToast(getActivity(), "Contraseña modificada exitosamente");
                    getActivity().finish();
                }
            }
        });
    }
}
